<script setup>

import { ref } from 'vue'
import {selectTitleExamListService,
    insertTitleExamListService,
    updateTitleExamListService,
    delTitleExamListService
} from '@/api/ExamManager.js'

const total = ref(0)
const params = ref({
  pageNum:1,
  pageSize:15
})

const selectTitleExamList = async () => {
    const res = await selectTitleExamListService(params.value)
    examTitleList.value = res.data.data
    total.value = res.data.total
}
selectTitleExamList()

const examTitleList = ref([])
const examTitleVoRef = ref()
const examTitleVo = ref({
      id:'',
      examId:'',
      examTitle:'',
      examType:'',
      examTypeStr:'',
      examOption:'',
      examOptionA:'',
      examOptionB:'',
      examOptionC:'',
      examOptionCorrect:'',
      examExtra:'',
      examPoint:'',
})
const rules = {
    examId:[{required:true,message:'请输入考试编号',trigger:'blur'}],
    examTitle:[{required:true,message:'请输入题目名称',trigger:'blur'}],
    examType:[{required:true,message:'请输入题目类型',trigger:'blur'}],
    examPoint:[{required:true,message:'请输入题目分值',trigger:'blur'}],
}

const handleClick = () => {
  console.log('click')
}


const showDrawer = ref(false)
const editInsertBtn = () => {
    showDrawer.value = true
    examTitleVo.value={}
}
const editRow = (row) => {
    examTitleVo.value = row
    showDrawer.value =  true
}
const delRow = async (row) => {
    await delTitleExamListService(row)
    ElMessage.success('成功！')
    selectTitleExamList()
}

const confirmClick = async()=>{
    await examTitleVoRef.value.validate()
    if(examTitleVo.value.id){
        await updateTitleExamListService(examTitleVo.value)
    }else{
        await insertTitleExamListService(examTitleVo.value)
    }

    ElMessage.success('成功！')
    examTitleVo.value={}
    showDrawer.value =  false
    selectTitleExamList()
}

</script>

<template>
  <div>
    <div class="cus-header">
        <el-card>
            <div style="display: flex;justify-content: space-between;">
                <div class="search">
                    <div>
                        课程：
                        <el-input v-model="input" style="width: 240px" placeholder="请输入课程名称" />
                    </div>
                    <div style="margin-left: 20px;">
                        课节：
                        <el-input v-model="input" style="width: 240px" placeholder="请输入课节" />
                    </div>
                    <div style="margin-left: 20px;">
                        姓名：
                        <el-input v-model="input" style="width: 240px" placeholder="请输入姓名" />
                    </div>
                    <div style="margin-left: 20px;">
                        <el-button style="width: 80px;" type="primary">搜索</el-button>
                    </div>
                </div>
                <div class="insert">
                    <el-button style="width: 80px;" type="primary" @click="editInsertBtn">新增</el-button>
                </div>
            </div>
            
            
        </el-card>
    </div>
    <div class="cus-body">
        <el-card style="min-height: 650px;">
            <el-table :data="examTitleList" style="width: 100%">
                <el-table-column fixed prop="id" label="题目编号" width="150" />
                <el-table-column prop="examId" label="考试编号" width="120" />
                <el-table-column prop="examTitle" label="题目名称" width="400" />
                <el-table-column prop="examTypeStr" label="题目类型" width="120" />
                <el-table-column prop="examOption" label="题目选项" width="120" />
                <el-table-column prop="examOptionCorrect" label="正确答案" width="120" />
                <el-table-column prop="examPoint" label="题目分值" width="320" />
                <el-table-column fixed="right" label="操作" min-width="120">
                    <template #default="{row}">
                        <el-button link type="primary" size="small" @click="editRow(row)">
                            编辑
                        </el-button>
                        <el-button link type="primary" size="small" @click="delRow(row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>
    </div>
  </div>

  <!-- 新增或编辑 -->
  <el-drawer v-model="showDrawer" >
        <template #header>
            <h4>操作</h4>
        </template>
        <template #default>
            <div>
                <el-form
                :model="examTitleVo" 
                ref="examTitleVoRef"
                :rules="rules"
                label-width="auto">
                    <el-form-item label="考试编号" :label-position="left" prop="examId">
                        <el-input v-model="examTitleVo.examId" />
                    </el-form-item>
                    <el-form-item label="考试题目" :label-position="left" prop="examTitle">
                        <el-input v-model="examTitleVo.examTitle" />
                    </el-form-item>
                    <el-form-item label="题目类型" :label-position="left" prop="examType">
                        <el-select v-model="examTitleVo.examType" placeholder="请选择">
                            <el-option label="选择题" value='1' />
                            <el-option label="问答题" value='2' />
                        </el-select>
                    </el-form-item>
                    <el-form-item v-if="examTitleVo.examType === '1'" label="选项A" :label-position="left" prop="examOptionA">
                        <el-input v-model="examTitleVo.examOptionA" />
                    </el-form-item>
                    <el-form-item v-if="examTitleVo.examType === '1'" label="选项B" :label-position="left" prop="examOptionB">
                        <el-input v-model="examTitleVo.examOptionB" />
                    </el-form-item>
                    <el-form-item v-if="examTitleVo.examType === '1'" label="选项C" :label-position="left" prop="examOptionC">
                        <el-input v-model="examTitleVo.examOptionC" />
                    </el-form-item>
                    <el-form-item label="正确答案" :label-position="left" prop="examOptionCorrect">
                        <el-input v-model="examTitleVo.examOptionCorrect" />
                    </el-form-item>
                    <el-form-item label="题目分数" :label-position="left" prop="examPoint">
                        <el-input v-model="examTitleVo.examPoint" />
                    </el-form-item>
                </el-form>
            </div>
        </template>
        <template #footer>
        <div style="flex: auto">
            <el-button @click="showDrawer = false">取消</el-button>
            <el-button type="primary" @click="confirmClick">提交</el-button>
        </div>
        </template> 
    </el-drawer>
</template>


<style lang="scss" scoped>
    .flex-row{
        display: flex;
        flex-direction: row;
    }
    .cus-header{
        .search{
            display: flex;
            flex-direction: row;
        }
    }
    .cus-body{
        margin-top: 15px;
    }
</style>